// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.IdPo;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.QualityCheckStorage;
import com.fowo.api.model.quality.check.storage.QualityCheckStorageItemVo;
import com.fowo.api.model.quality.check.storage.QualityCheckStorageSearchParamPo;
import com.fowo.api.model.quality.check.storage.QualityCheckStorageVo;
import com.fowo.api.service.QualityCheckStorageService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "质检单")
@RequestMapping("/api/quality-check-storage")
@RestController
public class QualityCheckStorageController {

  @Resource
  private QualityCheckStorageService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<QualityCheckStorageItemVo>> search(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<QualityCheckStorageItemVo>>> searchOptions(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(QualityCheckStorageSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<QualityCheckStorageItemVo>> getPage(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<QualityCheckStorageVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取质检单号相关的编号列表")
  @GetMapping("qc-sn/{texts}/ids")
  public R<List<Long>> getIdsByQcSn(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByQcSn(nameList));
  }

  @Operation(summary = "获取编号相关的质检单号列表")
  @GetMapping("qc-sn/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getQcSnByIds(idList));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody QualityCheckStorageVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody QualityCheckStorageVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody QualityCheckStorageVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageRevocation')")
  @Operation(summary = "撤销")
  @PostMapping("revocation")
  public R revocation(@RequestBody IdPo po) {
    return R.run(() -> service.revocation(po.getId()));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageBulkRevocation')")
  @Operation(summary = "批量撤销")
  @PostMapping("bulkRevocation")
  public R bulkRevocation(@RequestBody IdListPo po) {
    return R.run(() -> service.bulkRevocation(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageStockInGood')")
  @Operation(summary = "良品转移入库")
  @PostMapping("stockInGood")
  public R stockInGood(@RequestBody IdPo idPo) {
    return R.run(() -> service.stockInGood(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageStockInBad')")
  @Operation(summary = "次品转移入库")
  @PostMapping("stockInBad")
  public R stockInBad(@RequestBody IdPo idPo) {
    return R.run(() -> service.stockInBad(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageRevocationGood')")
  @Operation(summary = "良品撤销")
  @PostMapping("revocationGood")
  public R revocationGood(@RequestBody IdPo idPo) {
    return R.run(() -> service.revocationGood(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageRevocationBad')")
  @Operation(summary = "次品撤销")
  @PostMapping("revocationBad")
  public R revocationBad(@RequestBody IdPo idPo) {
    return R.run(() -> service.revocationBad(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageGoodExchange')")
  @Operation(summary = "良品库存转移")
  @PostMapping("goodExchange")
  public R goodExchange(@RequestBody IdPo idPo) {
    return R.run(() -> service.goodExchange(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('qualityCheckStorageBadExchange')")
  @Operation(summary = "次品库存转移")
  @PostMapping("badExchange")
  public R badExchange(@RequestBody IdPo idPo) {
    return R.run(() -> service.badExchange(idPo.getId()));
  }

  @Operation(summary = "全部")
  @GetMapping("all-quality-check")
  public R<List<QualityCheckStorageItemVo>> allQualityCheck(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allQualityCheck(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("all-quality-checkPage")
  public R<Page<QualityCheckStorageItemVo>> allQualityCheckPage(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allQualityCheckPage(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("all-quality-checkLastUpdateTime")
  public R<Date> allQualityCheckLastUpdateTime(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allQualityCheckLastUpdateTime(search));
  }

  @Operation(summary = "质检中")
  @GetMapping("qualitying")
  public R<List<QualityCheckStorageItemVo>> qualitying(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.qualitying(search));
  }

  @Operation(summary = "质检中分页")
  @GetMapping("qualityingPage")
  public R<Page<QualityCheckStorageItemVo>> qualityingPage(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.qualityingPage(search));
  }

  @Operation(summary = "质检中最后更新时间")
  @GetMapping("qualityingLastUpdateTime")
  public R<Date> qualityingLastUpdateTime(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.qualityingLastUpdateTime(search));
  }

  @Operation(summary = "待审批")
  @GetMapping("approval")
  public R<List<QualityCheckStorageItemVo>> approval(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.approval(search));
  }

  @Operation(summary = "待审批分页")
  @GetMapping("approvalPage")
  public R<Page<QualityCheckStorageItemVo>> approvalPage(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.approvalPage(search));
  }

  @Operation(summary = "待审批最后更新时间")
  @GetMapping("approvalLastUpdateTime")
  public R<Date> approvalLastUpdateTime(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.approvalLastUpdateTime(search));
  }

  @Operation(summary = "已完成")
  @GetMapping("completed")
  public R<List<QualityCheckStorageItemVo>> completed(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completed(search));
  }

  @Operation(summary = "已完成分页")
  @GetMapping("completedPage")
  public R<Page<QualityCheckStorageItemVo>> completedPage(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completedPage(search));
  }

  @Operation(summary = "已完成最后更新时间")
  @GetMapping("completedLastUpdateTime")
  public R<Date> completedLastUpdateTime(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.completedLastUpdateTime(search));
  }

  @Operation(summary = "已撤销")
  @GetMapping("revoked")
  public R<List<QualityCheckStorageItemVo>> revoked(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.revoked(search));
  }

  @Operation(summary = "已撤销分页")
  @GetMapping("revokedPage")
  public R<Page<QualityCheckStorageItemVo>> revokedPage(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.revokedPage(search));
  }

  @Operation(summary = "已撤销最后更新时间")
  @GetMapping("revokedLastUpdateTime")
  public R<Date> revokedLastUpdateTime(
    QualityCheckStorageSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.revokedLastUpdateTime(search));
  }

  @Operation(summary = "质检审核")
  @PutMapping("qualityInspectionAudit")
  public R<?> qualityInspectionAudit(@RequestBody JSONObject param) {
    return R.data(() -> service.qualityInspectionAudit(param));
  }

  @Operation(summary = "取消质检")
  @PutMapping("cancelQualityInspection")
  public R<?> cancelQualityInspection(@RequestBody JSONObject param) {
    return R.data(() -> service.cancelQualityInspection(param));
  }

  @Operation(summary = "更新库存")
  @PutMapping("updateInventoryPartStatistics")
  public R<?> updateInventoryPartStatistics(@RequestBody JSONObject param) {
    return R.data(() -> service.updateInventoryPartStatistics(param));
  }

  @Operation(summary = "完成质检")
  @PutMapping("completeQualityInspection")
  public R<?> completeQualityInspection(@RequestBody JSONObject param) {
    return R.data(() -> service.completeQualityInspection(param));
  }

  @Operation(summary = "详情页数据（废弃）")
  @PostMapping("detailPageData")
  public R<?> detailPageData(@RequestBody JSONObject param) {
    return R.data(() -> service.detailPageData(param));
  }

  @Operation(summary = "质检历史 ")
  @PostMapping("qualityInspectionHistory")
  public R<?> qualityInspectionHistory(@RequestBody JSONObject param) {
    return R.data(() -> service.qualityInspectionHistory(param));
  }

  @Operation(summary = "关联单据（收货单）")
  @PostMapping("associatedDocuments")
  public R<?> associatedDocuments(@RequestBody JSONObject param) {
    return R.data(() -> service.associatedDocuments(param));
  }

  @Operation(summary = "关联单据（入库单）	")
  @PostMapping("relatedDocuments")
  public R<?> relatedDocuments(@RequestBody JSONObject param) {
    return R.data(() -> service.relatedDocuments(param));
  }
}
